package com.itop.automator.weixin;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import com.itop.automator.R;
import com.itop.automator.commom.CommandUtils;
import com.itop.automator.commom.Constants;
import com.itop.automator.commom.Logger;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "MainActivity";

    private Button mStartBtn;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        inits();
    }

    private void inits() {
        mStartBtn = findViewById(R.id.btn_start);
        mStartBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_start:
                startWeiXinTestTask();
                break;
            default:
                break;
        }
    }

    private void startWeiXinTestTask() {
        new WeiXinTestTask().start();
    }

    /**
     * 运行uiautomator是个费时的操作，不应该放在主线程，因此另起一个线程运行
     */
    class WeiXinTestTask extends Thread {
        @Override
        public void run() {
            super.run();
            String command = generateCommand(Constants.PACKAGE_NAME_TEST_TOOL, ".weixin.WeiXinTest", "WeiXinTest");
            CommandUtils.CommandResult rs = CommandUtils.exec(command, true);
            Logger.d(TAG, "run: " + rs.error + "-------" + rs.success);
        }


        /*
        adb shell am instrument -w -r   -e debug false -e class com.itop.automator.weixin.WeiXinTest#startWeixin com.itop.automator.test/android.support.test.runner.AndroidJUnitRunner
         */
        public String generateCommand(String pkgName, String clsName, String funcName) {
            String command = "am instrument  --user 0 -w -r   -e debug false -e class "
                    + pkgName + "." + clsName + "#" + funcName + " "
                    + pkgName + "./android.support.test.runner.AndroidJUnitRunner";
            command = "am instrument  --user 0 -w -r   -e debug false -e class com.itop.automator.weixin.WeiXinTest#startWeixin com.itop" +
                    ".automator.test/android.support.test.runner.AndroidJUnitRunner";
            Logger.d(TAG, command);
            return command;
        }
    }

}
